package com.treetao.union.db.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.treetao.union.db.mapper.MallPSkuMapper;
import com.treetao.union.db.model.MallPSku;
import com.treetao.union.db.model.MallPSkuAttrItem;
import com.treetao.union.db.model.MallPSpu;
import com.treetao.union.db.service.MallPSkuAttrItemService;
import com.treetao.union.db.service.MallPSkuService;
import com.treetao.union.db.service.MallPSkuServiceService;
import com.treetao.union.db.service.MallPSpuService;

/**
 * <p>
 * 商品信息 服务实现类
 * </p>
 *
 * @author Dev.M
 * @since 2020-04-11
 */
@Service
public class MallPSkuServiceImpl extends ServiceImpl<MallPSkuMapper, MallPSku> implements MallPSkuService {
	@Autowired
	private MallPSkuAttrItemService mallPSkuAttrItemService;
	@Autowired
	private MallPSkuServiceService mallPSkuServiceService;
	@Autowired
	private MallPSpuService mallPSpuService;

	@Transactional
	@Override
	public boolean persist(MallPSku sku) {
		// 将所属SPU分类同步过来，便于查询
		if (sku.getSpuId() != null) {
			MallPSpu spu = mallPSpuService.getById(sku.getSpuId());
			if (spu != null) {
				sku.setRefOnecataId(spu.getRefOnecataId());
				sku.setRefTwocataId(spu.getRefTwocataId());
			}
		}

		// 保存SKU，会自动填充id
		boolean flag = save(sku);

		if (flag) {
			// 属性
			List<MallPSkuAttrItem> items = new ArrayList<MallPSkuAttrItem>();
			String[] attrs = sku.getAttrItemIds().split(",");
			for (String sattrItemId : attrs) {
				Long attrItemId = Long.valueOf(sattrItemId);
				MallPSkuAttrItem item = new MallPSkuAttrItem();
				item.setRefAttrItemId(attrItemId);
				item.setRefSkuId(sku.getId());// 同批次所有属性，sku一致
				items.add(item);
			}
			mallPSkuAttrItemService.saveBatch(items);

			// 服务保障
			List<com.treetao.union.db.model.MallPSkuService> services = new ArrayList<com.treetao.union.db.model.MallPSkuService>();
			String[] arrService = sku.getServiceIds().split(",");
			for (String serviceIds : arrService) {
				Long serviceId = Long.valueOf(serviceIds);
				com.treetao.union.db.model.MallPSkuService item = new com.treetao.union.db.model.MallPSkuService();
				item.setRefServiceId(serviceId);
				item.setRefSkuId(sku.getId());// 同批次所有属性，sku一致
				services.add(item);
			}

			mallPSkuServiceService.saveBatch(services);
			return true;
		}

		return false;
	}

	@Transactional
	@Override
	public boolean modify(MallPSku sku) {
		// 将所属SPU分类同步过来，便于查询
		if (sku.getSpuId() != null) {
			MallPSpu spu = mallPSpuService.getById(sku.getSpuId());
			if (spu != null) {
				sku.setRefOnecataId(spu.getRefOnecataId());
				sku.setRefTwocataId(spu.getRefTwocataId());
			}
		}
		// 保存SKU，会自动填充id
		boolean flag = updateById(sku);
		if (flag) {
			// 需要先删除该SKU对应的所有属性值 ，然后再次添加
			QueryWrapper<MallPSkuAttrItem> q = new QueryWrapper<MallPSkuAttrItem>();
			q.eq("ref_sku_id", sku.getId());
			mallPSkuAttrItemService.remove(q);

			List<MallPSkuAttrItem> items = new ArrayList<MallPSkuAttrItem>();
			String[] attrs = sku.getAttrItemIds().split(",");
			for (String sattrItemId : attrs) {
				Long attrItemId = Long.valueOf(sattrItemId);
				MallPSkuAttrItem item = new MallPSkuAttrItem();
				item.setRefAttrItemId(attrItemId);
				item.setRefSkuId(sku.getId());// 同批次所有属性，sku一致
				items.add(item);
			}
			mallPSkuAttrItemService.saveBatch(items);

			// 服务保障，需要先删除该SKU对应的所有服务保障 ，然后再次添加
			QueryWrapper<com.treetao.union.db.model.MallPSkuService> qSevice = new QueryWrapper<com.treetao.union.db.model.MallPSkuService>();
			q.eq("ref_sku_id", sku.getId());
			mallPSkuServiceService.remove(qSevice);

			List<com.treetao.union.db.model.MallPSkuService> services = new ArrayList<com.treetao.union.db.model.MallPSkuService>();
			String[] arrService = sku.getServiceIds().split(",");
			if(arrService!=null && arrService.length>0) {
				for (String serviceIds : arrService) {
					Long serviceId = Long.valueOf(serviceIds);
					com.treetao.union.db.model.MallPSkuService item = new com.treetao.union.db.model.MallPSkuService();
					item.setRefServiceId(serviceId);
					item.setRefSkuId(sku.getId());// 同批次所有属性，sku一致
					services.add(item);
				}
			}
			mallPSkuServiceService.saveBatch(services);
			return true;
		}
		return false;
	}

	@Override
	public MallPSku getSku(String skuNo) {
		QueryWrapper<MallPSku> q = new QueryWrapper<MallPSku>();
		q.eq("skuno", skuNo);
		return getOne(q);
	}

}
